package com.game.utils
{
	import flash.display.LoaderInfo;
	import flash.display.MovieClip;
	import flash.system.ApplicationDomain;
	
	/**
	 * 
	 * @author chery
	 */
	public class SwfManager
	{
		private static var _this:SwfManager=new SwfManager()
		private var _allAppList:Array=[]
		private var _allSwfClass:Array=[]
		private var _allSWF:Object={}
		/**
		 * 管理加载的swf文件
		 */
		public function SwfManager()
		{
			if(_this){
				
			}else{
				_this=this;
			}
		}
		/**
		 * 获取实例
		 * @return 
		 */
		public static function getInstance():SwfManager{
			if(_this==null){
				_this=new SwfManager();
			}
			return _this;
		}
		/**
		 * 添加已加载完成的Loaderinfo
		 * @param _loaderInfo
		 */
		public function addApp(_loaderInfo:LoaderInfo,loadURL:String=null):void{
			_allAppList.push(_loaderInfo.applicationDomain);
			if(loadURL==null){
				return;
			}
			while(loadURL.indexOf("\\")!=-1)//将"\"都转换为"/"
				loadURL=loadURL.replace("\\","/");
			
			//保存至动态对象中
			_allSWF[loadURL]=_loaderInfo.content;
		}
		/**
		 * 通过链接名获取元件
		 * @param _className
		 * @return 
		 */
		public function getMovieClip(_className:String):MovieClip{
			if(_allSwfClass[_className]){
				
				return  new _allSwfClass[_className]();
			}else{
				for each(var app:ApplicationDomain in _allAppList){
					if(app.hasDefinition(_className)){;
						var c:Class=app.getDefinition(_className) as Class;
						_allSwfClass[_className]=c;
						return new c();
					}
				}
				return null;
			}
		}
		public function getSWFByUrl(_url:String):*{
			return _allSWF[_url];
		}
		
	}
}